/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.h5.controller.ware;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.UserFavoritesPo;
import com.itttq.gshop.domain.po.UserInfoPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.CatBrandTo;
import com.itttq.gshop.generic.connector.IUserCartConnector;
import com.itttq.gshop.generic.connector.IUserFavoritesConnector;
import com.itttq.gshop.generic.connector.IWareCatBrandConnector;
import com.itttq.gshop.generic.connector.IWareInfoConnector;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants.WareStatus;

@RestController
@RequestMapping("ware")
public class WareInfoController extends GenericController {
	private static final long serialVersionUID = -7435776176064932L;
	public static final String cacheKey = "ware";
	@Autowired
	private IWareInfoConnector wareInfoConnector;
	@Autowired
	private IWareCatBrandConnector wareCatBrandConnector;
	@Autowired
	private IUserCartConnector userCartConnector;
	@Autowired
	private IUserFavoritesConnector userFavoritesConnector;
	/**
	 * 商品列表
	 * 
	 * @param cid	分类ID，如果为空则默认获取第一个分类下的品牌数据展示
	 * @param bid	品牌ID，如果为空，分类不为空时则按分类获取数据展示
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("list.html")
    public ModelAndView getWares(Integer cid, Integer bid) {
		ModelAndView mv = new ModelAndView("/product/products");
		Map<String, List<CatBrandTo>> catBrands = wareCatBrandConnector.getAllEnableCatBrands();
		if(catBrands.isEmpty()) return mv;
		CatBrandTo cbt = catBrands.entrySet().iterator().next().getValue().get(0);
		WareInfoPo ware = new WareInfoPo();
		if(null == cid) { // 没有穿参数则默认获取第一个分类品牌下的商品数据
			ware.setCategoryId(cbt.getCatId());
			ware.setBrandId(cbt.getBrandId());
		} else {
			ware.setCategoryId(cid);
			if(null != bid) ware.setBrandId(bid);
		}
		mv = getWares(ware);
		mv.addObject("ware", ware);
		mv.addObject("catBrandMap", catBrands);
		mv.setViewName("/product/products");
        return mv;
    }
	
	/**
	 * 商品列表
	 * 
	 * @param ware	查询对象
	 * 
	 * @Author 迷蝶
	 * @return ModelAndView
	 */
	@RequestMapping("wares.html")
	public ModelAndView getWares(WareInfoPo ware) {
		ModelAndView mv = new ModelAndView("/product/products-content");
		ware.setStatus(WareStatus.ONSHELF.getVal());
		mv.addObject("wares", wareInfoConnector.getWares(ware));
		mv.addObject("catId", ware.getCategoryId());
		mv.addObject("brandId", ware.getBrandId());
        return mv;
    }
	
	@RequestMapping("categories.html")
    public ModelAndView getCategoryAndBrand() {
		ModelAndView mv = new ModelAndView("/product/category");
		mv.addObject("catBrandMap", wareCatBrandConnector.getAllEnableCatBrands());
        return mv;
    }
	
	@RequestMapping("/{id}.html")
	public ModelAndView getWareInfo(@PathVariable("id") int id, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/product/detail");
		mv.addObject("ware", wareInfoConnector.getWareInfo(id));
		UserInfoPo user = this.getUserFromSession(request);
		if(null != user) {
			try {
				mv.addObject("cartWares", userCartConnector.getWareSumByUserId(user.getId()));
				// 获取收藏信息,TODO GXG 根据商品ID和用户ID获取
				UserFavoritesPo fav = new UserFavoritesPo();
				fav.setUserId(user.getId());
				fav.setWareId(id);
				mv.addObject("favorite", userFavoritesConnector.isFavorite(id, user.getId()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//TODO GXG  获取推荐商品
		WareInfoPo ware = new WareInfoPo();
		ware.setSize(10);
		mv.addObject("wares", wareInfoConnector.getWareInfos(ware).getData());
		return mv;
	}

	@RequestMapping("/save")
	@CacheEvict(value=cacheKey, key="'wareInfo_'+#ware.id")
	public Integer saveWareInfo(WareInfoPo ware) {
		return wareInfoConnector.saveWareInfo(ware);
	}

	@RequestMapping("/del/{id}")
	@CacheEvict(value=cacheKey, key="'wareInfo_'+#id")
	public Integer delWareInfo(@PathVariable("id") int id) {
		return wareInfoConnector.delWareInfo(id);
	}
}